//go:build linux

package encsdk

import (
	"gitee.com/fhe-search/fhe-search-rpc/encsdk/cardsdk"
	"gitee.com/fhe-search/fhe-search-rpc/encsdk/softsdk"
)

var (
	pEncryptType string
)

func SetEncryptType(encryptType string) {
	pEncryptType = encryptType
}

func EncInit(secretKeyBasePath string) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		softsdk.SoftEncInit(secretKeyBasePath)
	} else {
		cardsdk.UseHardWare(secretKeyBasePath)
		cardsdk.GetX0(secretKeyBasePath)
	}
}

func SymEncUtf8(utf8ClearText string) ([]byte, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.AesEncUtf8(utf8ClearText)
	} else {
		return cardsdk.SM1LongUtf8Enc(utf8ClearText)
	}
}

func SymDecUtf8(utf8EncText []byte) (string, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.AesDecUtf8(utf8EncText)
	} else {
		return cardsdk.SM1LongUtf8Dec(utf8EncText)
	}
}

func SymEncS32Ex(int32Arr []int32) ([][]byte, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.AesEncS32Ex(int32Arr)
	} else {
		return cardsdk.PEncS32ExWrapper(int32Arr)
	}
}

func SymEncS32(plain int32) ([]byte, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.AesEncS32(plain)
	} else {
		return cardsdk.PEncS32Wrapper(plain)
	}
}

func SymDecS32(encData []byte) (int32, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.AesDecS32(encData)
	} else {
		return cardsdk.PDecS32Wrapper(encData)
	}
}

func FheEncUtf8(utf8Text string) ([]byte, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.FheSoftEncUtf8(utf8Text)
	} else {
		return cardsdk.FHEUtf8StrEnc(utf8Text)
	}
}

func FheDecUtf8(utf8EncText []byte) (string, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.FheSoftDecUtf8(utf8EncText)
	} else {
		return cardsdk.FHEUtf8StrDec(utf8EncText)
	}
}

func FheSub(enc1, enc2 []byte) (diff []byte, err error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.FheSoftSub(enc1, enc2)
	} else {
		return cardsdk.FheMultiSub(enc1, enc2)
	}
}

func FheDecDiff(diff []byte) (bool, error) {
	if !(pEncryptType == "soft_encrypt" || pEncryptType == "card_encrypt") {
		panic("pEncryptType must be 'soft_encrypt' or 'card_encrypt', but you give " + pEncryptType)
	}
	if pEncryptType == "soft_encrypt" {
		return softsdk.FheSoftDecDiff(diff)
	} else {
		return cardsdk.FheMultiDiffDec(diff)
	}
}
